package com.frxuexi.park2210

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.webkit.JavascriptInterface
import com.frxuexi.park2210.databinding.ActivityWebViewBinding
import com.tencent.smtt.sdk.WebView

class WebViewActivity : AppCompatActivity() {
    lateinit var binding:ActivityWebViewBinding
    lateinit var mywv:WebView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding= ActivityWebViewBinding.inflate(layoutInflater)
        setContentView(binding.root)

        mywv=binding.mywv

        mywv.apply {
            loadUrl("file:///android_asset/test.html")
            //允许和js交互
            settings.javaScriptEnabled=true
            //js调安卓步骤1.
            addJavascriptInterface(this@WebViewActivity,"android")
        }

        binding.btnCalljs.setOnClickListener {
            //安卓调用js1.
            mywv.loadUrl("javascript:harmonyCallJS()")
        }

    }

    //js调安卓步骤2.
    @JavascriptInterface
    fun harmonyosAgree():String{
        return "hi"
    }

    override fun onDestroy() {
        super.onDestroy()
        mywv.clearHistory()
        mywv.destroy()
    }
}